#include <stdio.h>
#include "src/password_generator.h"

int main(int argc, char * argv[])
{
	if ( argc == 2 )
	{
		char * file_address = argv[1];
		FILE * accounts_csv = fopen(file_address, "r");
		if (accounts_csv == NULL)
		{
			printf("Failed to open: %s\n", file_address);
			return 1;
		}
		// Get keyword for input
		const char keyword[4096];
		printf("Enter keyword: ");
		scanf("%s", keyword);
		
		char line_buffer[1024], service_buffer[1024], username_buffer[1024];
		int length, special;
		
		while ( fgets(line_buffer, 1024, accounts_csv) )
		{
			if ( sscanf(line_buffer, "%[^,],%[^,],%d,%d", service_buffer, username_buffer, &length, &special) != 4)
			{
				continue;
			}

			char password[length + 1];
			generate_password(password, keyword, service_buffer, username_buffer, length, special);

			printf("/--------------------------------%s\n%s\n%s\n\n", 
					service_buffer,
					username_buffer,
					password);
		}
		fclose(accounts_csv);	
	}
	else if (argc == 5)
	{

		const char * service_buffer  = argv[1];
		const char * username_buffer = argv[2];
		int length, special;
		sscanf(argv[3], "%d", &length);
		sscanf(argv[4], "%d", &special);
		// Get keyword for input
		char keyword[4096];
		printf("Enter keyword: ");
		scanf("%s", keyword);
		
		char password[length + 1];
		generate_password(password, keyword, service_buffer, username_buffer, length, special);

		printf("/--------------------------------%s\n%s\n%s\n\n", 
			service_buffer,
			username_buffer,
			password);
	}
}
